<template>
  <view>
    <view class="header">
      <view class="back" @click="back()">
        <uni-icons type="left" size="20" color="#fff"></uni-icons>
      </view>
      <text>搜索商品</text>
    </view>
    <view class="input-box">
      <uni-search-bar @input="input" bgColor="#fff" :radius="100" cancelButton="none"
        placeholder="请输入搜索内容"></uni-search-bar>
    </view>

    <view class="goods-list">
      <block v-for='(item,index) in searchList' :key='index'>
        <goods-box :item='item' @chooseGoods='chooseGoods'></goods-box>
      </block>
    </view>

    <uni-popup ref="popup" type="bottom" mask-background-color='rgba(0,0,0,0.9)'>
      <buy-goods :addressInfo='addressInfo' :goodsCount='goodsCount' :goodsDetail='goodsDetail' @closePopup='closePopup'
        @changeCount='changeCount'></buy-goods>
    </uni-popup>
  </view>
  </view>
</template>

<script>
  import {
    mapState
  } from 'vuex'
  export default {
    data() {
      return {
        kw: '',
        timer: null,
        searchList: [],
        addressInfo: {},
        goodsDetail: {
          colorVOS: []
        },
        goodsCount: 1,
      };
    },
    created() {
      this.getAddressInfo()
    },
    onShow() {
      let that = this
      uni.$on('addressInfo', function(data) {
        that.addressInfo = JSON.parse(data)
      })
    },
    methods: {
      back() {
        uni.navigateBack()
      },
      input(e) {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.kw = e
          if (this.kw.trim() != '') this.getSearchList()
        }, 500)
      },
      async getAddressInfo() {
        const {
          data: {
            data
          }
        } = await uni.$http.get(`/users/address/getDefault/${this.userInfo.studentId}`)
        this.addressInfo = data
      },
      async getSearchList() {
        const {
          data: {
            data
          }
        } = await uni.$http.get(`/users/product/productName?productName=${this.kw}`)
        this.searchList = data
        console.log(data);
      },
      open() {
        this.$refs.popup.open('bottom')
      },
      closePopup() {
        this.$refs.popup.close()
      },
      async chooseGoods(id) {
        const {
          data: {
            data
          }
        } = await uni.$http.get(`/users/product/${id}`)
        this.goodsDetail = data
        this.open()
      },
      changeCount(e) {
        this.goodsCount = e
      },
    },
    computed: {
      ...mapState('my_user', ['userInfo']),
    }
  }
</script>

<style lang="scss">
  .header {
    position: relative;
    width: 100%;
    height: 140rpx;
    text-align: center;
    font-size: 36rpx;
    font-weight: 700;
    line-height: 170rpx;
    background-color: #b6edaa;
    color: #fff;

    .back {
      position: absolute;
      top: 4rpx;
      left: 23rpx;
      height: 50rpx;
    }
  }

  .input-box {
    background-color: #f5f5f5;
  }

  .goods-list {
    display: flex;
    flex-wrap: wrap;
    margin-top: 20rpx;
  }
</style>